package com.example.travel.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.text.Html;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.travel.R;
import com.example.travel.bean.Place;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebActivity extends AppCompatActivity {
    EditText editText,editText2,editText3;
    Button button;
    TextView tv;
//    Place cityPlace;
    Place cityPlace;
    String ws="http://ws.webxml.com.cn/WebServices/DomesticAirline.asmx/getDomesticAirlinesTime?";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        init();
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                String place=editText.getText().toString().trim();
                String place1=editText2.getText().toString().trim();
                String time=editText3.getText().toString().trim();
                if(place==null|place.equals("")){
                    Toast.makeText(getApplicationContext(),"请输入!",Toast.LENGTH_SHORT)
                            .show();
                }else{
                    new AsyncTask<String,Void,Place>(){
                        @Override
                        protected Place doInBackground(String... params) {
                            // ArrayList<String> name2 = new ArrayList<>();
//                            String phoneInfor="";
                            String strParams="startCity="+params[0]+"&lastCity="+params[1]+"&theDate="+params[2]+"&userID=";
                            ws=ws+strParams;
                            try {
                                URL wsUrl=new URL(ws);
                                //1.
                                HttpURLConnection con=(HttpURLConnection) wsUrl.openConnection();
                                con.setRequestMethod("GET");
                                con.setRequestProperty("connection", "keep-alive");
                                con.setDoInput(true);
                                con.setReadTimeout(5000);

                                int code=con.getResponseCode();
                                System.out.println("code:"+code);
                                if(code==200){
//                                    cityPlace=new Place();
                                    cityPlace=new Place();
                                    InputStream is=con.getInputStream();
                                    XmlPullParser xpParse= Xml.newPullParser();
                                    xpParse.setInput(is,"utf-8");
                                    int type=xpParse.getEventType();
                                    int index=1;
                                    while (type!=XmlPullParser.END_DOCUMENT){
                                        if(type==XmlPullParser.START_TAG){
                                            if(xpParse.getName().equals("Company")){
                                                cityPlace.setCityName(xpParse.nextText());

                                            }
                                            if(xpParse.getName().equals("StartDrome")){
                                                cityPlace.setWeather(xpParse.nextText());

                                            }
                                            if(xpParse.getName().equals("ArriveDrome")){
                                                cityPlace.setLife(xpParse.nextText());

                                            }
                                            if(xpParse.getName().equals("StartTime")){
                                                cityPlace.setNote(xpParse.nextText());

                                            }
                                            if(xpParse.getName().equals("ArriveTime")){
                                                cityPlace.setCap(xpParse.nextText());

                                            }

                                        }
                                        type=xpParse.next();
                                    }
                                    is.close();
                                    con.disconnect();
                                }else{

                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return cityPlace;
                        }
                        @Override
                        protected void onPostExecute(Place cityPlace){
//                            String htmlStr="<font >"+
//                                    cityPlace.getCityName()
//                                    +"</font><br>"+
//                                    "<font >"+
//                                    cityPlace.getWeather()
//                                    +"</font><br>"+
//                                    "<font >"+
//                                    cityPlace.getLife()
//                                    +"</font><br>"+
//                                    "<font >"+
//                                    cityPlace.getNote()
//                                    +"</font><br>"+
//                                    "<font >"+
//                                    cityPlace.getCap()
//                                    +"</font><br>";
                            String htmlStr   = cityPlace.getCityName() ;
                            String htmlStr1  = cityPlace.getWeather();
                            String htmlStr2  = cityPlace.getLife();
                            String htmlStr3  = cityPlace.getNote();
                            String htmlStr4  = cityPlace.getCap();
//                            tv.setText(Html.fromHtml(htmlStr));
                            Intent intent = new Intent(WebActivity.this, WebActivity2.class);
                            intent.putExtra("date", htmlStr);
                            intent.putExtra("date1", htmlStr1);
                            intent.putExtra("date2", htmlStr2);
                            intent.putExtra("date3", htmlStr3);
                            intent.putExtra("date4", htmlStr4);
                            startActivity(intent);
//
                            finish();
                        }

                    }.execute(place,place1,time);
                }


            }

        });
    }
    public void init(){
        editText=(EditText)this.findViewById(R.id.et_Place);
        editText2=(EditText)this.findViewById(R.id.et_Place1);
        editText3=(EditText)this.findViewById(R.id.et_Time);
        button=(Button)this.findViewById(R.id.bt_search);
        tv=(TextView)this.findViewById(R.id.tv);
    }

}
